package com.lovo.stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.bean.SupplierBean;
import com.lovo.stock.constant.MessageCode;
import com.lovo.stock.service.ISupplierService;
import com.lovo.stock.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 供应商
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    ISupplierService supplierService;

    /**
     * 分页查找供应商
     * @param pageInfo
     * @return IPage<SupplierBean>
     */
    @RequestMapping("/querySupplierByPage")
    public ResponseResult<IPage<SupplierBean>> querySupplierByPage(@RequestBody PageInfo<SupplierBean> pageInfo){
        ResponseResult<IPage<SupplierBean>> responseResult=new ResponseResult<>();
        try {
            IPage<SupplierBean> page= supplierService.querySupplierByPage(pageInfo);
            responseResult.setData(page);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 根据id查找供应商
     * @param id
     * @return
     */
    @RequestMapping("/querySupplierInfoById")
    public ResponseResult<SupplierBean> querySupplierInfoById(int id){
        ResponseResult<SupplierBean> responseResult=new ResponseResult<>();
        try {
            SupplierBean supplierBean=supplierService.querySupplierInfoById(id);
            responseResult.setData(supplierBean);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;

    }

    /**
     * 查询所有供应商
     * @return
     */
    @RequestMapping("/queryAllSupplier")
    public ResponseResult<List<SupplierBean>> queryAllSupplier(){
        ResponseResult<List<SupplierBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(supplierService.queryAllSupplier());
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    @RequestMapping("/updateSupplier")
    public ResponseResult updateSupplier(@RequestBody SupplierBean supplierBean){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            supplierService.updateSupplier(supplierBean);
            return responseResult;
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
            return responseResult;
        }
    }

    /**
     * 添加供应商
     * @param supplierBean
     * @return
     */
    @RequestMapping("/addSupplier")
    public ResponseResult addSupplier(@RequestBody SupplierBean supplierBean){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            supplierService.addSupplier(supplierBean);
            return responseResult;
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
            return responseResult;
        }
    }

    @RequestMapping( "/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        supplierService.importExcel(file);
    }

}
